<?php
class ControllerAccountDownload extends AccountController {
	public function index() {

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['text_downloads'],
			'href'      => US('account/download'),
        	'separator' => $this->_['text_separator']
      	);

		M('account/download', 'download');

		$download_total = $this->download->getTotalDownloads();

		if ($download_total) {
			
			$page = (int) G('page', 1);

			$this->data['downloads'] = array();
			$results = $this->download->getDownloads(($page - 1) * C('config_catalog_limit'), C('config_catalog_limit'));

			$suffix = array(
					'B',
					'KB',
					'MB',
					'GB',
					'TB',
					'PB',
					'EB',
					'ZB',
					'YB'
			);
			
			foreach ($results as $result) {
				if (file_exists(DIR_DOWNLOAD . $result['filename'])) {
					$size = filesize(DIR_DOWNLOAD . $result['filename']);

					$i = 0;

					while (($size / 1024) > 1) {
						$size = $size / 1024;
						$i++;
					}

					$this->data['downloads'][] = array(
						'order_id'   => $result['order_id'],
						'date_added' => date($this->_['date_format_short'], strtotime($result['date_added'])),
						'name'       => $result['name'],
						'remaining'  => $result['remaining'],
						'size'       => round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
						'href'       => US('account/download/download', 'order_download_id=' . $result['order_download_id'])
					);
				}
			}

			$this->data['pagination'] = $this->pagination($download_total, $page)->render();

			$this->display('account/download.tpl');
		} else {
			$this->data['heading_title'] = $this->_['heading_title'];
			$this->data['text_error'] = $this->_['text_empty'];
			$this->data['continue'] = US('account/account');
			
			$this->display('error/not_found.tpl');
		}
	}

	public function download() {

		M('account/download', 'download');
		
		$order_download_id = (int) G('order_download_id', 0);
		
		$download_info = $this->download->getDownload($order_download_id);

		if ($download_info) {
			$file = DIR_DOWNLOAD . $download_info['filename'];
			$mask = basename($download_info['mask']);

			if (!headers_sent()) {
				if (file_exists($file)) {
					header('Content-Type: application/octet-stream');
					header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
					header('Expires: 0');
					header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
					header('Pragma: public');
					header('Content-Length: ' . filesize($file));
					
					if (ob_get_level()) ob_end_clean();
					
					readfile($file, 'rb');

					$this->download->updateRemaining($order_download_id);

					exit;
				} else {
					exit('Error: Could not find file ' . $file . '!');
				}
			} else {
				exit('Error: Headers already sent out!');
			}
		} else {
			$this->redirect(US('account/download'));
		}
	}
}
?>